import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '设备名称',		name: 'device_id', width:250, type: 'select', loading:true, onValue:option => $fn.searchDevice.call(this,option) },
			{ label: '设备型号',		name: 'device_model' },
			{ label: '设备识别码 ',	name: 'id_code' },
			{ label: '购买时间',		names: ['buy_date_start_date','buy_date_end_date'], type:'date-range' },
			{ label: '设备类型',		name: 'device_type', type: 'select', loading:true, idStr: 'code' },
		],
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
		$fn.fresh.on(this.getSelect)
	}
	
	componentWillUnmount(){
		$fn.fresh.remove()
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 获取设备名称
			$fn.setRow( forms, 'device_id', await f.getDivCache())
			// 获取设备类型
			$fn.setRow( forms, 'device_type', await f.getDicCache({ code: 43500 }))
			//
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'device/index')
	
	// 表单
	cols = [
		{ title: '编号', 		dataIndex: 'device_number', 	width:150 },
		{ title: '设备名称', 	dataIndex: 'device_name', 		width:200 },
		{ title: '设备型号', 	dataIndex: 'device_model', 		width:200 },
		{ title: '设备识别码', 	dataIndex: 'id_code', 			width:200 },
		{ title: '购买时间', 	dataIndex: 'buy_date', 			...$fn.table.time },
		{ title: '采购价格', 	dataIndex: 'buy_price', 		...$fn.table.num  },
		{ title: '设备类型', 	dataIndex: 'device_type_name', 	width:150 },
		
		{ title: '所属科室', 	dataIndex: 'project_name', 		width:150 },
		{ title: '负责人', 		dataIndex: 'pinyin_name', 		...$fn.table.name  },
		{ title: '启用时间', 	dataIndex: 'enabeld_date', 		...$fn.table.time },
		{ title: '联机类型', 	dataIndex: 'rel_type_name', 	...$fn.table.status },
		{ title: '设备状态', 	dataIndex: 'device_status_name', ...$fn.table.status },
		TableControls.controls.call(this,{ width: 400 })
	]
	// 表格操作按钮
	table_controls = (text,{uuid, device_id, device_name}) => {
		return [
			TableControls.pushTo.call(this, '编辑', `device-add?uuid=${uuid}`),
			TableControls.pushTo.call(this, '校准计划', `device-plan?uuid=${uuid}`),
			TableControls.pushTo.call(this, '校准记录', `calibration-record?uuid=${uuid}`),
			TableControls.pushTo.call(this, '项目关联', `project-related?device_id=${uuid}&device_name=${$fn.encode(device_name)}`),
			TableControls.pushTo.call(this, '设备维护', `device-maintain?uuid=${uuid}`),
			
		]
	}
	// 操作按钮
	controls = () => [
		Controls.pushTo.call(this, '添加', 'device-add' ),
		Confirm.call(this, '禁用', 'device/delete', { isKeys: 1 }),
		Controls.imports.call(this,'device/import', {op_key: 'DeviceExcel', reconfirm: true}),						//导入
		// 下载导入模板
		Controls.importTemplate.call(this),
	]
	
	render(){
		return (
			<Page title='设备列表' controls={this.controls()}>
				{/* 搜索 */}
				{ SearchForm.call(this) }
				{/* 表格 */}
				{ Table.call(this,{isInit:window.isInit}) }
			</Page>
		)
	}
}

export default Index